﻿using Serilog;
using Serilog.Events;

namespace KAs.Logger;

public static class LoggerInit
{
    public static void Initialization()
    {
        string Logformat = @"{Timestamp:yyyy-MM-dd HH:mm-dd }[{Level:u3}] {Message:lj}{NewLine}";
        string warningOutputTemplate = "{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff} LogLevel：{Level}{NewLine}{Message}{NewLine}" + new string('-', 50) + "{NewLine}";

        Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()//最小的输出单位是Debug级别的
        .MinimumLevel.Override("Microsoft", LogEventLevel.Error)//将Microsoft前缀的日志的最小输出级别改成Information
        .Enrich.FromLogContext()
        .WriteTo.Console(outputTemplate: Logformat)
        .WriteTo.Map("FileName", @"Default/log", (fileName, lsConfig) =>
            lsConfig.Logger(lg =>
                lg.WriteTo.File(LogFilePath(fileName), rollingInterval: RollingInterval.Day,
                    outputTemplate: fileName == @"Default/log" ? Logformat : warningOutputTemplate)
            )
        )
        .CreateLogger();
    }
    private static string LogFilePath(string fileName)
    {
        return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"Logs/{fileName}.log");
    }
}